package com.graduation_design.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation_design.entity.AppCategory;
import com.graduation_design.entity.AppProduct;
import com.graduation_design.entity.AppSkuStock;
import com.graduation_design.entity.AppSpecificationValue;
import com.graduation_design.service.AppCategoryService;
import com.graduation_design.service.AppProductService;
import com.graduation_design.mapper.AppProductMapper;
import com.graduation_design.service.AppSkuStockService;
import com.graduation_design.service.AppSpecificationValueService;
import com.sun.org.apache.bcel.internal.generic.DUP;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
* @author 从南
* @description 针对表【app_product】的数据库操作Service实现
* @createDate 2024-02-16 18:15:58
*/
@Service
public class AppProductServiceImpl extends ServiceImpl<AppProductMapper, AppProduct>
    implements AppProductService{

    @Resource
    AppCategoryService appCategoryService;

    @Resource
    AppSpecificationValueService appSpecificationValueService;

    @Resource
    AppSkuStockService appSkuStockService;

    @Resource
    AppProductMapper appProductMapper;

    /**
     * 商品分类
     * 排序字段: 0->默认；1->按新品；2->按销量；
     */
    @Override
    public Object pageByCategoryId(Page page, Long categoryId, Integer sort) {

        AppCategory category = appCategoryService.getById(categoryId);
        Assert.notNull(category, "商品分类不存在");

        QueryWrapper<AppProduct> wrapper = new QueryWrapper<AppProduct>()
                .eq("is_on_sale", true)
                .eq("category_id", categoryId)
                .orderByDesc(sort == 0, "is_top", "sort_order")
                .orderByDesc(sort == 1, "created")
                .orderByDesc(sort == 2, "sale");

        Page<AppProduct> pages = this.page(page, wrapper);

        return MapUtil.builder()
                .put("category", category)
                .put("pageData", pages)
                .build();
    }

    /**
     * 把商品信息，以及相关的信息都查出来
     */
    @Override
    public AppProduct getWithAttrsById(Long productId) {

        // 商品详情
        AppProduct product = this.getById(productId);
        Assert.notNull(product, "该商品不存在");

        // 商品属性 [ { "id": 28, "spec": "颜色", "value": "金色;银色;白色"... } ... ]
        List<AppSpecificationValue> specificationValues = appSpecificationValueService.listByProductId(productId);
        product.setSpecifications(specificationValues);

        // 商品可选组合(sku stock price) sku: 商品库存最小的存货单位
        // [ { "id": 117, "price": 397977, "stock": 99, "sku": "金色;32G", "lockStock": 0, "specs": [ "金色", "32G" ], "isDelete": false }]
        List<AppSkuStock> skuStocks = appSkuStockService.listSkuByProductId(productId);
        product.setSkuStocks(skuStocks);

        return product;
    }

    @Override
    public Page<AppProduct> pageWithCategory(Page page, String name, Long categoryId) {
        return appProductMapper.pageWithCategory(page, name, categoryId);
    }

    @Override
    public void create(AppProduct appProduct) {
        if (appProduct.getId() == null) {
            appProduct.setSn(generateProductSn());
            appProduct.setCreated(LocalDateTime.now());
        } else {
            appProduct.setUpdated(LocalDateTime.now());
        }

        this.saveOrUpdate(appProduct);
    }

    private String generateProductSn() {
        String dateStr = DateUtil.format(new Date(), "yyMMddHHmmss");
        return "PD" + dateStr + RandomUtil.randomNumbers(4);
    }
}




